package com.hooya.fa.eu.biz.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.hooya.fa.eu.biz.dao.entity.eu.his.TempPkpy;
import com.hooya.fa.eu.biz.dao.mapper.eu.his.HisTempPkpyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/3/19
 * @Version 1.0.0
 */
@Slf4j
public class PkpyHisListener implements ReadListener<TempPkpy> {

    private HisTempPkpyMapper hisTempPkpyMapper;

    private static final int BATCH_COUNT = 1000;

    private List<TempPkpy> cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private Date createTime;

    public PkpyHisListener(HisTempPkpyMapper hisTempPkpyMapper) {
        this.hisTempPkpyMapper = hisTempPkpyMapper;
        this.createTime = new Date();
    }

    @Override
    public void invoke(TempPkpy data, AnalysisContext context) {
        data.setCreateTime(createTime);
        cacheData.add(data);
        if (cacheData.size() >= BATCH_COUNT) {
            addTempPkpyList();
            cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        addTempPkpyList();
    }

    public void addTempPkpyList() {
        if (!CollectionUtils.isEmpty(cacheData)) {
            hisTempPkpyMapper.insertBatchSomeColumn(cacheData);
        }
    }
}
